﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XmlStudio.Logger.Interfaces;

namespace XmlStudio.Logger
{
    /// <summary>
    /// Actual log implementation. Implement ILog interface. This class is used from static class Log.
    /// </summary>
    internal class LogImplementor : ILog
    {
        #region Fields
        private ILogFactory factory;
        private string path;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the LogImplementor class.
        /// </summary>
        /// <remarks>This constructor is internal so it cannot be called directly from another assembly.</remarks>
        internal LogImplementor()
        {
        }
        #endregion

        #region Methods
        /// <summary>
        /// Starts the log.
        /// </summary>
        /// <param name="path">Path to the log file to write to.</param>
        /// <returns>Reference to the created log.</returns>
        public ILog Start(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("path is null or empty");
            }

            if (this.factory != null)
            {
                throw new ApplicationException("log has alredy been started.");
            }

            this.path = path;
            this.factory = new LogFactory();
            factory.Create(path);

            return this;
        }

        /// <summary>
        /// Stops the log.
        /// </summary>
        public void DisposeLog()
        {
            if (this.factory.Current != null)
            {
                this.factory.DisposeOfLogWriter();
                this.factory = null;
                this.path = string.Empty;
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the reference to the current log writer.
        /// </summary>
        /// <exception cref="System.ApplicationException">When current log writer is null.</exception>
        public ILogWriter CurrentLogWriter
        {
            get
            {
                if (this.factory == null)
                {
                    throw new ApplicationException("log not started");
                }

                if (this.factory != null && this.factory.Current == null)
                {
                    throw new ApplicationException("log not started");
                }
                else
                {
                    return this.factory.Current;
                }
            }
        }

        /// <summary>
        /// Gets the value indicating whether the log is started.
        /// </summary>
        public bool IsStarted
        {
            get { return this.factory != null; }
        }

        /// <summary>
        /// Gets the path to the log file.
        /// </summary>
        public string Path
        {
            get
            {
                if (this.factory != null)
                {
                    return this.path;
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        #endregion
    }
}